package springbook.user.dao;

import java.sql.SQLException;

import org.junit.Test;
import org.junit.runner.JUnitCore;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

import springbook.user.domain.User;

/**
 * 유저를 등록하고 불러오는 기능 테스트 클래스
 * 
 * */
public class UserDaoTest {
	@Test
	public void addAndGet() throws SQLException, ClassNotFoundException{
		ApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml");
		UserDao userDao = context.getBean("userDao", UserDao.class);
		User user = new User("dwshin1", "shin dong won1", "12345");
		User user2 = new User("dwshin2", "shin dong won2", "67890");

		userDao.deleteAll();
		assertThat(userDao.getCount(), is(0));
		
		userDao.add(user);		//유저를 등록 하는 메소드
		userDao.add(user2);		//유저를 등록 하는 메소드
		assertThat(userDao.getCount(), is(2));
		
		
		User userget1 = userDao.get(user.getId());
		assertThat(userget1.getName(), is(user.getName()));
		assertThat(userget1.getPassword(), is(user.getPassword()));
		
		User userget2 = userDao.get(user2.getId());
		assertThat(userget2.getName(), is(user2.getName()));
		assertThat(userget2.getPassword(), is(user2.getPassword()));
	}
	@Test
	public void count() throws SQLException, ClassNotFoundException{
		ApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml");
		UserDao userDao = context.getBean("userDao", UserDao.class);
		User user1 = new User("dwshin1", "shin dong won1", "12345");
		User user2 = new User("dwshin2", "shin dong won2", "12345");
		User user3 = new User("dwshin3", "shin dong won3", "12345");

		userDao.deleteAll();
		assertThat(userDao.getCount(), is(0));
		
		userDao.add(user1);		//유저를 등록 하는 메소드
		assertThat(userDao.getCount(), is(1));
		userDao.add(user2);		//유저를 등록 하는 메소드
		assertThat(userDao.getCount(), is(2));
		userDao.add(user3);		//유저를 등록 하는 메소드
		assertThat(userDao.getCount(), is(3));
	}
	public static void main(String[] args){
		JUnitCore.main("springbook.user.dao.UserDaoTest");
	}
}
